package com.hugeyurt.service.impl;

import com.hugeyurt.mapper.CourseMapper;
import com.hugeyurt.po.Course;
import com.hugeyurt.po.EnrollCourse;
import com.hugeyurt.service.CourseService;
import com.hugeyurt.util.QueryPools;
import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CourseServiceImpl implements CourseService
{
    @Resource
    private CourseMapper courseMapper;

    @Resource
    private RedisTemplate  redisTemplate;

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Override
    public Course getCourse(Integer cno) {
        return courseMapper.getCourseById(cno);
    }

    @Override
    public Boolean saveEnrollCourse(EnrollCourse enrollCourse) {
        return courseMapper.saveEnrollCourse(enrollCourse);
    }
   /*
    @Override
    public  synchronized Boolean secondKill(String sno, Integer cno)
    {
            Course course = courseMapper.getCourseById(cno);
            Integer numbers = courseMapper.getEnrollNumbers(cno);//获得已选该课程的人数
            if (numbers < course.getCount()) {
                EnrollCourse enrollCourse = new EnrollCourse();
                enrollCourse.setCno(cno);
                enrollCourse.setSno(sno);
                enrollCourse.setTime(getTime());
                courseMapper.saveEnrollCourse(enrollCourse);
                return true;
            }
            return false;

    }


    @Override
    public  synchronized  Boolean secondKill(String sno, Integer cno)
    {

        if(QueryPools.snoSet.contains(sno))
        {
            System.out.println("你已经选过课了");
            return false;
        }
        Course course = QueryPools.courseMap.get(cno.toString());
        if(QueryPools.snoSet.size()<course.getCount())
        {
            QueryPools.snoSet.add(sno);
            EnrollCourse enrollCourse = new EnrollCourse();
            enrollCourse.setCno(cno);
            enrollCourse.setSno(sno);
            enrollCourse.setTime(getTime());
            courseMapper.saveEnrollCourse(enrollCourse);
            return true;
        }
        System.out.println("课程已经选满了。。。。");
        return false;
    }
    */
   public   Boolean secondKill(String sno, Integer cno)
   {
      //判断是否已经选过课
       boolean isExisted=redisTemplate.opsForSet().isMember("user:"+cno.toString(),sno);
       if(isExisted)
       {
           System.out.println("你已经选过这门课，不能重复选");
           return false;
       }
       //课程是否被选满了
       Integer ok=(Integer)redisTemplate.opsForList().leftPop("cno:"+cno.toString());
       if(ok!=null)
       {
          //把选课信息发送消息队列中去
           redisTemplate.opsForSet().add("user:"+cno.toString(),sno);
           sendEnrollCourse(sno,cno);
           System.out.println("你成功选到了该门课程");
           return true;

       }
       System.out.println("该门课程,已经选满了");
       return true;
   }

    @Override
    public Boolean initSkillEvent()
    {
        Integer id=courseMapper.checkStatus(getTime());
        if(id==null)  return false;
        courseMapper.updateEnrollEvent(id);// 更新事件转态
       // Course course = courseMapper.getCourseById(1);// 等到选课的课程信息
        List<Course> lists=courseMapper.getAllCourse();
        for(int i=0;i<lists.size();i++) {
            Course course=lists.get(i);
           // QueryPools.courseMap.put(course.getCno().toString(), course);
           redisTemplate.opsForValue().set(course.getCno().toString(),course);
            for(int j=0;j<course.getCount();j++)
                redisTemplate.opsForList().rightPush("cno:"+course.getCno().toString(),j);
        }
        return true;
    }

    private String getTime()
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date());
    }

    private void sendEnrollCourse(String sno, Integer cno)
    {
        HashMap<String,String> map = new HashMap<String,String>();
        map.put("sno",sno);
        map.put("cno",cno.toString());
        rabbitTemplate.convertAndSend("course-exchange",null,map);
    }
}
